<template>
	<view class="page">
		<view class="general-view margin-lr padding radius-16" style="margin-top: 32rpx;padding: 20rpx;">
			<view class="exchange" style="padding-bottom: 32rpx;">
				<view class="flex align-center justify-between">
					<view class="flex justify-between align-center datalist">
						<!-- <text class="text-bold text-333">店铺数据</text> -->
						<input placeholder="请输入兑换码" v-model="picke_code" />
					</view>
					<view class="" style="font-size: 28rpx;color:#FC6296;margin-left: 24rpx;" @click="copy">
						粘贴
					</view>
				</view>

				<view class="flex align-center justify-between" style="margin-top: 32rpx;">
					<view class="flex justify-between align-center datalist" style="width: 70%;">
						<input placeholder="请输入验证码" v-model="verify_code" maxlength="6" />
					</view>
					<view class="" style="font-size: 28rpx;color:#FC6296;margin-left: 24rpx;" @click="handleSendVerCode">
						{{ beginning ? time + 's后重新获取' : '发送验证码' }}
					</view>
				</view>
				<view style="color: red;margin-top: 24rpx;font-size: 24rpx;">*请向购买者索要验证码，并输入验证码</view>		


			</view>
			<!-- 兑换商品 -->
			<!-- <view class="bg-white padding">
            <view class="bg-list">
                兑换商品
            </view>
			<view class="flex" style="justify-content: space-between;">
				<view class="flex text-center margin-top">
					<image src="./static/2.png" mode="" style="width: 140rpx;height: 140rpx;margin-right: 16rpx;"></image>
					<view class="conten">
                        <view style="font-size: 28rpx;font-family: PingFang SC-Medium, PingFang SC;font-weight: 500;color: #333333;">七分甜】好喝的青山白柚·乌龙</view>
                        <view class="flex conten">
                            <view class="flex" style="align-items: center">
                                <image src="./static/9.png" mode="" style="width: 22rpx;height: 22rpx;margin-right: 16rpx;"></image>
                                <text style="font-size: 20rpx;">无需预约</text>
                            </view>
                            <view class="flex" style="align-items: center;margin-left: 12rpx;">
                                <image src="./static/9.png" mode="" style="width: 22rpx;height: 22rpx;margin-right: 16rpx;"></image>
                                <text style="font-size: 20rpx;">随时退</text>
                            </view>
                            <view class="flex" style="align-items: center;margin-left: 12rpx;">
                                <image src="./static/9.png" mode="" style="width: 22rpx;height: 22rpx;margin-right: 16rpx;"></image>
                                <text style="font-size: 20rpx;">积分抵</text>
                            </view>
                        </view>
                         <view class="flex conten">
                            <view class="flex" style="align-items: center">
                                <text style="font-size: 28rpx;font-family: Lato-Bold, Lato;font-weight: bold;color: #FF5C5C;margin-right: 16rpx;">￥.60</text>
                                <text style="font-size: 20rpx;text-decoration: line-through;">￥17</text>
                            </view>
                            
                        </view>
                    </view>
				</view>
			</view>
			
		</view> -->
		</view>
		<view class="btn_cloce">
			<view class="btn" @click="duihuan">立即兑换</view>
		</view>

		<fu-popup v-model="visible" mode="center" :maskClick="true">

			<view class="wrap">
				<view class="coupon_content flex flex-wrap justify-center">
					<view class="">
						兑换商品
					</view>
					<scroll-view scroll-y="true" style="height:620rpx;">
						<view class="flex" style="justify-content: space-between;" v-for="(item, index) in goodsList"
							:key="index">
							<view class="flex margin-top" style="align-items: center;">
								<view class="margin-right-32 flex-shrink" @click="selectGoods(item)">
									<image src="/static/checked.png" mode="scaleToFill"
										style="width: 36rpx;height: 36rpx;" v-if="item.isSelect" />
									<image src="/static/unchecked.png" mode="scaleToFill"
										style="width: 36rpx;height: 36rpx;" v-else />
								</view>
								<image :src="item.goods_thumb" mode=""
									style="width: 140rpx;height: 140rpx;margin-right: 24rpx;flex-shrink: 0;border-radius: 16rpx;">
								</image>
								<view class="conten">
									<view
										style="font-size: 28rpx;font-family: PingFang SC-Medium, PingFang SC;font-weight: 500;color: #333333;    overflow: hidden;white-space: nowrap;text-overflow: ellipsis;width:390rpx;">
										{{item.goods_name}}</view>
									<view class="flex conten">
										<view class="flex" style="align-items: center">
											<image src="https://xcxadmin.jgwxch.com/h5img/image/010.png" mode=""
												style="width: 22rpx;height: 22rpx;margin-right: 16rpx;"></image>
											<text style="font-size: 20rpx;
	color: #666666;">无需预约</text>
										</view>
										<!-- <view class="flex" style="align-items: center;margin-left: 12rpx;">
	                                <image src="https://xcxadmin.jgwxch.com/h5img/image/010.png" mode="" style="width: 22rpx;height: 22rpx;margin-right: 16rpx;"></image>
	                                <text style="font-size: 20rpx;
	color: #666666;">随时退</text>
	                            </view> -->
										<view class="flex" style="align-items: center;margin-left: 12rpx;">
											<image src="https://xcxadmin.jgwxch.com/h5img/image/010.png" mode=""
												style="width: 22rpx;height: 22rpx;margin-right: 16rpx;"></image>
											<text style="font-size: 20rpx;
	color: #666666;">积分抵</text>
										</view>
									</view>
									<view class="flex conten">
										<view class="flex" style="align-items: center">
											<text
												style="font-size: 28rpx;font-family: Lato-Bold, Lato;font-weight: bold;color: #FF5C5C;margin-right: 16rpx;">￥{{item.shop_price}}</text>
											<text style="font-size: 20rpx;text-decoration: line-through;
	color: #999999;">￥{{item.market_price}}</text>
										</view>

									</view>
								</view>
							</view>
						</view>
					</scroll-view>
					<!-- 立即领取 -->
					<view class="footer-bottom text-lg" @click="exchange">
						立即兑换
					</view>
				</view>
			</view>
			<view style="width: 100%;margin-top: 70rpx;" class="flex justify-center">
				<!-- <image @click="visible = false" src="./static/close.png" style="width: 64rpx;height: 64rpx;" mode="">
				</image> -->
			</view>
		</fu-popup>
		<!-- 网络检测 start -->
		<fu-notwork></fu-notwork>
		<!-- 网络检测 end -->
	</view>
</template>

<script>
	import richText from "@/common/utils/richText.js";
	export default {
		data() {
			return {
				isShowLoading: false, //页面加载loading
				id: "",
				detail: "",
				content: "",
				body: "",
				picke_code: '',
				visible: false,
				goodsList: [],
				isCheck: false,
				verify_code: '',
				time: 60, // 倒计时60S,@Number
				beginning: false, // 显示倒计时,@Boolean
				timer: null, // 计时器,@Function
				isLoading:false
			};
		},
		onLoad(options) {
			this.id = options.id;
		},
		onShow() {},
		computed: {
			write_off_num() {
				let arr = this.goodsList.filter(item => item.isSelect)
				return arr.length
			}
		},
		methods: {
			/**
			 * @event 发送验证码
			 */
			handleSendVerCode() {
				// 如果按钮状态是加载中，直接return，防止用户多次点击
				if (!this.picke_code) {
					this.$message.info('请输入兑换码')
					return
				}
				if (this.isLoading) return;
				// 如果有倒计时，return false
				if (this.beginning) return;
				// 上述验证都通过，将按钮的点击状态改为true
				this.isLoading = true;
				/**
				 * @desc 请求验证码接口
				 * @param {Object} {} - 参数
				 * @param {Boolean} {}.is_test - 是否是测试环境
				 * @param {String} {}.mobile - 手机号
				 * @param {String} {}.type - 类型
				 */
				this.$api
					.post(global.apiUrls.postCancelCode, {
						picke_code: this.picke_code
					})
					.then(res => {
						// 如果后台返回的code码为1
						if (res.data.code == 1) {
							this.beginning = true;
							// 清除倒计时,防抖作用
							clearInterval(this.timer);
							this.timer = setInterval(() => {
								if (this.time == 1) {
									//倒计时结束就清楚这个倒计时
									clearInterval(this.timer);
									this.time = 60; // 倒计时60s
									this.beginning = false; // 是否显示倒计时改为false
									return;
								}
								this.time--;
							}, 1000);
							// 向用户发送提示
							if (res.data.data.code) {
								if (global.IS_DEV == 1) {
									this.$message.info(this.i18n['验证码为'] + res.data.data.code + this.i18n['请注意查收']);
									this.vercode = res.data.data.code;
								} else {
									this.$message.info('验证码发送成功，请注意查收')
								}
							}
							// 把按钮的状态改为false
							this.isLoading = false;
						} else {
							this.beginning = false; // 是否显示倒计时改为false
							// 把按钮的状态改为false
							this.isLoading = false;
							// 如果报错，向用户提示
							this.$message.info(res.data.msg);
						}
					})
					.catch(function(err) {
						// 把按钮的状态改为false
						this.isLoading = false;
					});
			},
			duihuan() {
				if (this.isCheck) return
				if (!this.picke_code) {
					this.$message.info('请输入兑换码')
					return
				}
				// if (!this.verify_code) {
				// 	this.$message.info('请输入验证码')
				// 	return
				// }
				this.isCheck = true
				this.$api.post(global.apiUrls.post64058d51a7f80, {
					picke_code: this.picke_code
				}).then(res => {
					if (res.data.code == 1) {
						console.log(res.data.data)
						let data = res.data.data
						data.forEach(item => {
							item.isSelect = true
						})
						this.goodsList = data
						this.isCheck = false
						this.visible = true
					} else {
						this.isCheck = false
						this.$message.info(res.data.msg)
					}
				}).catch(() => {
					this.isCheck = false
				})
			},
			// 立即兑换
			exchange() {
				if (this.isCheck) return;
				if (this.write_off_num < 1) {
					this.$message.info('请选择兑换商品')
					return
				}
				this.isCheck = true
				this.$api.post(global.apiUrls.post640590e4aaa78, {
					picke_code: this.picke_code,
					write_off_num: this.write_off_num,
					sms_code:this.verify_code
				}).then(res => {
					if (res.data.code == 1) {
						this.$message.info(res.data.msg)
						this.picke_code = ''
						this.visible = false
					}
					setTimeout(() => {
						this.isCheck = false
					}, 800)
				}).catch(() => {
					this.isCheck = false
				})
			},
			selectGoods(item) {
				console.log(item)
				item.isSelect = !item.isSelect
			},
			copy() {
				uni.getClipboardData({
					success: (res) => {
						// this.$message.info('复制成功');
						if (!res.data) {
							this.$message.info('剪切板暂无内容');
						}
						this.picke_code = res.data
					}
				})
			}
		},
	};
</script>

<style lang="scss">
	.page {
		// background-color: white;
		height: 100%;
		height: 100vh;
	}

	/* 文章标题 start */
	.title {
		padding: 32upx 32upx 0;
		/* height: 99rpx; */
		font-size: 32rpx;
		font-family: PingFang SC;
		font-weight: 500;
		line-height: 54rpx;
		color: #333333;
		opacity: 1;
	}

	.time {
		padding: 0 32upx;
		height: 33rpx;
		font-size: 24rpx;
		font-family: PingFang SC;
		font-weight: 400;
		line-height: 33rpx;
		color: #999999;
		opacity: 1;
		margin-top: 18rpx;
	}

	/* 文章标题 end */

	/* 文章详情 start */
	.article-detail {
		margin: 32upx 32upx 24upx;
		color: #666666;
		font-size: 28upx;
		line-height: 48upx;
	}

	/* 文章详情 end */
	.active {
		border-radius: 44rpx;
		text-align: center;
		border: 1rpx solid #FC6296;
		padding: 8rpx 24rpx;
		color: #FC6296;
	}

	.exchange {
		background: #fff;
		padding: 56rpx 32rpx;
		text-align: center;
		border: 16rpx;
		margin-bottom: 32rpx;

		.datalist {
			width: 85%;
			height: 88rpx;
			background: #F6F7F9;
			border-radius: 60rpx;
			text-align: center;
			justify-content: center;
		}
	}

	.bg-list {
		text-align: center;
		font-size: 32rpx;
		font-family: PingFang SC-Bold, PingFang SC;
		font-weight: bold;
		color: #333333;
	}

	.conten {
		margin: 16rpx 0;
	}

	.btn_cloce {
		position: fixed;
		left: 32rpx;
		right: 32rpx;
		bottom: 50rpx;
		background-color: #FC6296;

		// border: 1rpx solid #BFBFBF;
		border-radius: 55rpx;

		.btn {
			height: 88rpx;
			text-align: center;
			line-height: 88rpx;
			color: #fff;
			font-size: 32rpx;
		}
	}

	.wrap {
		width: 680rpx;
		overflow: hidden;
		background-size: 100%;
		background-repeat: no-repeat;
		// background: url(~@/static/discount.png)no-repeat;
		background-color: #FFFFFF;
		background-size: 100% 100%;
		height: 898rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		border-radius: 16rpx;

		.close_Popup {
			position: fixed;
			right: 30rpx;
			top: 30rpx;
			font-size: 48rpx;
		}

		.coupon_content {
			// width: 540rpx;
			font-size: 42rpx;
			// height: 100%;
			// padding: 300rpx 50rpx 20rpx 20rpx;
			// padding-top: 32rpx;
			padding: 24rpx;

			.coupon_title {
				padding-bottom: 40rpx;
			}

			.items {
				width: 100%;
				height: 144rpx;
				background-size: 100% 100%;
				display: flex;
				margin-bottom: 20rpx;

				.left {
					width: 170rpx;
					height: 100%;
					display: flex;
					justify-content: center;
					align-items: center;
					font-size: 46rpx;
					color: #D9361E;
				}

				.center {
					flex: 1;
					display: flex;
					flex-direction: column;
					// margin-left: 10rpx;

					text {
						font-size: 28rpx;
						color: #333333;
					}

					view {
						font-size: 20rpx;
						color: #693313;
					}
				}

				.right {
					width: 120rpx;
					height: 100%;
					display: flex;
					align-items: center;
					justify-content: center;

					.buttons {
						width: 80rpx;
						color: #FC6296;
						font-size: 30rpx;
						line-height: 46rpx;
						text-align: center;
					}

					.disabled {
						opacity: 0.5;
					}
				}
			}
		}
	}

	.footer-bottom {
		background: #FC6296;
		border-radius: 44rpx;
		color: #FFFFFF;
		padding: 22rpx 96rpx;
		margin-top: 64rpx;
	}
</style>